//
//  ChangeUserInfoViewController.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/8/29.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit
import RxCocoa

class ChangeUserInfoViewController: SettingItemViewController {
    
    let country = TableViewItem.init(title: "国家".localized)
    let name = TableViewItem.init(title: "姓名".localized)
    let phone = TableViewItem.init(title: "电话".localized)
    let company = TableViewItem.init(title: "公司名称".localized)
    let job = TableViewItem.init(title: "职位".localized)
    let website = TableViewItem.init(title: "公司网站".localized)
    let remark = TableViewItem.init(title: "备注".localized)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "修改个人信息".localized
        tableView.backgroundColor = #colorLiteral(red: 0.9724436402, green: 0.972609818, blue: 0.9724331498, alpha: 1)
    
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "完成".localized, style: .plain, target: self, action: #selector(save))
        
        country.cellSelect = { [unowned self] in
            self.selectCountry()
        }
        name.textFieldSetUp = { _ in }
        phone.textFieldSetUp = { _ in }
        company.textFieldSetUp = { $0.placeholder = "非必填".localized }
        job.textFieldSetUp = { $0.placeholder = "非必填".localized }
        website.textFieldSetUp = { $0.placeholder = "非必填".localized }
        remark.textFieldSetUp = { $0.placeholder = "非必填".localized }
        
        ApiManager.userInfo().loadCache()
            .dataModelMap(PersonalModel.self)
            .subscribe(onNext: { [weak self] (model) in
                self?.country.detailTitle = model.country
                self?.name.textFieldValue = model.name
                self?.phone.textFieldValue = model.phone
                self?.company.textFieldValue = model.companyName
                self?.job.textFieldValue = model.position
                self?.website.textFieldValue = model.companyWebsite
                self?.remark.textFieldValue = model.remarks
                self?.tableView.reloadData()
            }).disposed(by: rx.disposeBag)
        
        tableView.keyboardDismissMode = .onDrag
    }
    
    func selectCountry() {
        self.view.endEditing(true)
        let picker = CountrySelectView()
        picker.selectCountry = { [weak self] in
            self?.country.detailTitle = $0
            self?.tableView.reloadData()
        }
        picker.showAnimation()
    }
    
    @objc func save() {
        self.view.endEditing(true)
        guard country.detailTitle.count > 0,
            name.textFieldValue.count > 0,
            phone.textFieldValue.count > 0 else {
                return
        }
        
        ApiManager.updateUserInfo(email: nil, name: name.textFieldValue, phone: phone.textFieldValue, country: country.detailTitle, company: company.textFieldValue, job: job.textFieldValue, companyWebsite: website.textFieldValue, remarks: remark.textFieldValue)
            .request()
            .trackingLoading()
            .filterCode200()
            .post(.userInfoRefresh)
            .showSuccess(text: "修改成功".localized)
            .subscribe()
            .disposed(by: rx.disposeBag)
    }
    
    override func sectionArray() -> [TableViewSection] {
        return [TableViewSection.section(items: [country, name, phone, company, job, website, remark])]
    }
    
    override func navigationBarStyle() -> NavigationBarStyle {
        return .baseColor
    }
}
